<template>
  <yt-page title="安全风险列表" :rightData="rightData">
    <yt-group ref="group" :searchApi="searchApi" :group="false">
      <yt-card slot-scope="scope"
        btn="查看更多详情"
        @btn-click="goDetailPage(scope)"
        class="no-padding"
        >
        <yt-cell border="none" label="工艺/单元" :value="scope.name"></yt-cell>
        <yt-cell border="none" label="事故类型" :value="scope.type"></yt-cell>
        <yt-cell border="none" label="L/E/C" :value="getValue(scope.contents)"></yt-cell>
        <yt-cell border="none" label="D(L*E*C)" :value="getValue2(scope.contents)"></yt-cell>
        <yt-cell border="none" label="风险等级" :value="scope.level"></yt-cell>
        <yt-cell border="none" label="风险内容" :value="scope.des"></yt-cell>
      </yt-card>
    </yt-group>
    <yt-popup pos="right" v-model="search" :mask="true" slot="alert">
      <yt-page class="page" :flex="true" :leftData="[]" title="筛选条件">
        <yt-form>
          <!--<yt-inlineSelect title="登记时间" v-model="condition.time" :list="TIME" :x="3" label="key" val="endTime"></yt-inlineSelect>-->
          <yt-inlineSelect title="风险等级" v-model="condition.level" :list="levelDic" :x="3" label="D_text" val="D_code" multiple></yt-inlineSelect>
          <yt-inlineSelect title="事故类型" v-model="condition.type" :list="disasterDic" :x="3" label="D_text" val="D_code" multiple></yt-inlineSelect>
          <yt-inlineSelect title="因素类别" v-model="condition.genre" :list="riskTypeDic" :x="3" label="D_text" val="D_code" multiple></yt-inlineSelect>
          <!-- <yt-inlineSelect title="评估标准" v-model="condition.criteria" :list="SecurityRiskControlCriteriaDic" :x="4" label="name" val="id" multiple></yt-inlineSelect> -->
          <yt-btn-group slot="btn" :shadow="false">
            <yt-btn theme="white" @click="search = false">关闭</yt-btn>
            <yt-btn @click="onSearch">确定</yt-btn>
          </yt-btn-group>
        </yt-form>

      </yt-page>
    </yt-popup>
  </yt-page>
</template>

<script>
import { commonApi } from '@/common/api/commonApi.js'
import { ReplaceDictionary } from '@/common/utils/index.js'
import { formatTime } from '@/common/plugin/index.js'
import moment from 'moment'
const TIME = [
  { key: '近三天', endTime: moment().subtract(3, 'days').format('YYYY-MM-DD') },
  { key: '近一周', endTime: moment().subtract(7, 'days').format('YYYY-MM-DD') },
  { key: '近一个月', endTime: moment().subtract(30, 'days').format('YYYY-MM-DD') },
  { key: '近三个月', endTime: moment().subtract(90, 'days').format('YYYY-MM-DD') },
  { key: '近半年', endTime: moment().subtract(180, 'days').format('YYYY-MM-DD') },
  { key: '近一年', endTime: moment().subtract(365, 'days').format('YYYY-MM-DD') }
]
const LEVEL = [
  { D_code: '1级', D_text: '一级风险' },
  { D_code: '2级', D_text: '二级风险' },
  { D_code: '3级', D_text: '三级风险' },
  { D_code: '4级', D_text: '四级风险' }
]
export default {
  data () {
    return {
      TIME,
      rightData: [{
        font: 'yt-icon yt-icon-search',
        click: () => {
          this.search = !this.search
        }
      }],
      search: false,
      condition: {
        time: '',
        type: [],
        genre: [],
        criteria: [],
        level: ''
      },
      disasterDic: [], // 事故类型
      riskTypeDic: [], // 因素类别
      levelDic: LEVEL, // 风险等级
      SecurityRiskControlCriteriaDic: [], // 评估标准
      searchApi: async ({ skip, limit }) => {
        this.condition.time = ''
        let {type = [], genre = [], criteria = [], time, level = []} = this.condition
        // todo 数据库里的time为啥都是undefine，这样会导致查询出错
        type = type.length ? { type } : {}
        genre = genre.length ? { genre } : {}
        criteria = criteria.length ? { criteria } : {}
        level = level.length ? { level } : {}

        let condition = {...type, ...criteria, ...genre, ...level}
        let timeQueryOption = time ? { greaterThanOrEqualTo: { time: time }, lessThanOrEqualTo: { time: moment().format('YYYY-MM-DD') } } : {}
        let param = Object.assign({ equalTo: this.$route.query, containedIn: condition, ...timeQueryOption }, { skip, limit, include: ['user'] })
        let p = [
          commonApi.dictionary('disaster'),
          commonApi.dictionary('risk_type'),
          commonApi.search('security_risk_corporate', param)
        ]
        let [disaster, riskType, data] = await Promise.all(p)
        this.disasterDic = disaster
        this.riskTypeDic = riskType

        return ReplaceDictionary(data, { type: disaster })
      }
    }
  },
  activated () {
    this.init()
  },
  methods: {
    async init () {
      if (this.SecurityRiskControlCriteriaDic.length) return
      this.SecurityRiskControlCriteriaDic = await commonApi.search('SecurityRiskControlCriteria', {
        ascending: 'order', equalTo: { 'active': true }
      })
    },
    onSearch () {
      this.search = false
      this.$refs.group.startPull()
    },
    goDetailPage (item) {
      this.$router.$push({ path: '/security-risk/detail', query: {objectId: item.id, gridId: item.gridId ? item.gridId.id : ''} })
    },
    getValue (obj) {
      if (!obj) return ''
      return `${obj[0].score}/${obj[1].score}/${obj[2].score}`
    },
    getValue2 (obj) {
      if (!obj) return ''
      return obj[0].score * obj[1].score * obj[2].score
    },
    initCondition() {
      this.condition = {
        time: '',
        type: [],
        genre: [],
        criteria: []
      }
    }
  }
}
</script>

<style lang="stylus" scoped>
  >>> .flex
    display flex
    justify-content space-between
    padding 10px
    border-bottom .5px solid #f5f5f5
  >>> .no-padding .yt-cell-label
    padding 0
</style>
